<template>
    <n-upload
      :action="BASE+'/upload'"
      name="avatar"
      @finish="handelfinish"
    >
      <n-button boolean type="primary">上传头像</n-button>
    </n-upload>
</template>
<script setup>
import {
    NButton,
    NUpload,
} from 'naive-ui'
import { BASE } from '../config';
import { useMessage } from 'naive-ui';
import { useAccount } from '../store/account';
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue']) // 此处作用等同于this.$emit('update:modelValue')
const account = useAccount()
const message = useMessage();
const handelfinish = async (options)=>{
    let res = JSON.parse(options.event.target.response)
    console.log(res);
    let {code,msg,path} = res
    let msgState = ['error','success']
    message[msgState[code]](msg)//弹出提示框
    account.userInfo.avatar = path//本地更新
    emit('update:modelValue',path)//将图片路径传给当前组件的调用者v-model
    if(res.code == 1) {
        await account.updateUserInfo(
            account.jwttoken,
            account.userInfo._id,
            { avatar: path } // 明确只更新头像字段
        )
    }
}
</script>